import Vue from 'vue'
import App from './App.vue'
import router from './router'
import {Button, RadioGroup, Radio, Icon, NavBar, Search, Toast, TabbarItem, Tabbar, Swipe, SwipeItem,
  Tag, Uploader, Tab, Tabs, Popup, Lazyload, Field, DatetimePicker, CollapseItem, 
  Collapse, Cell, CellGroup, Checkbox, CheckboxGroup, List, Dialog, Overlay,
  PullRefresh, Loading, Area
} from 'vant'
import '@/assets/common.css'
import '@/assets/vant-reset.css'

import store from './store'
import vanEmpty from '@/components/vanEmpty.vue'

Vue.use(Button).use(Icon).use(NavBar).use(Search).use(Toast).use(Tabbar).
use(TabbarItem).use(Swipe).use(SwipeItem).use(Tag).use(Uploader).use(Tab).
use(Tabs).use(Popup).use(Field).use(DatetimePicker).use(Collapse).use(CollapseItem).
use(Cell).use(CellGroup).use(CheckboxGroup).use(Checkbox).use(Dialog).
use(Overlay).use(Radio).use(RadioGroup).use(List).use(Lazyload).use(PullRefresh).
use(Loading).use(Area)

// Vue.prototype.$Toast = Toast;
Vue.prototype.$back = () => {history.back()};
Vue.config.productionTip = false
Vue.prototype.$baseUrl = 'http://sto.joyou.com/'
Vue.prototype.$baseImg = 'http://sto.joyou.com/uploads/'

Vue.component('van-empty', vanEmpty)

Vue.prototype.$getCookie = name => {
  var strcookie = document.cookie;//获取cookie字符串
  var arrcookie = strcookie.split("; ");//分割
  //遍历匹配
  for ( var i = 0; i < arrcookie.length; i++) {
      var arr = arrcookie[i].split("=");
      if (arr[0] == name){
          return arr[1];
      }
  }
  return "";
}

Vue.filter('formatTime',(time,config='YMD')=>{
  var myTime = time*1000;
  // 'hour12':false 用来表示
  var totalTime = new Date(myTime).toLocaleString('chinese',{'hour12':false}).replace(' ','/');
  var year = totalTime.split('/')[0]
  var month = ('0' + totalTime.split('/')[1]).slice(-2)
  var day = ('0' + totalTime.split('/')[2]).slice(-2)
  var hour = totalTime.split('/')[3]
  // 显示年月日
  if(config=='YMDH'){
    return `${year}-${month}-${day} ${hour}`
  }else if(config=='YMD'){
    return `${year}-${month}-${day}`
  }
})

// 注册一个全局守卫（导航卫士）,其作用就是在路由跳转之前，对路由进行判断，防止未登录的用户跳转到需要登录的页面
// router.beforeEach((to, from, next) => {
//   let account = Vue.prototype.$getCookie('mobile') //获取用户登录时生成的token
//   if (account) {
//       next(); //如果token存在，说明用户已经登录，调用next方法，不妨碍用户继续浏览
//   } else {
//       if (to.path !== '/mine/login') { //如果用户没有登录浏览别的页面，则强制跳转到登录页面
//           next({ path: '/mine/login' })
//       } else { //如果用户浏览的就是登录页面，则调用next方法 ，不去干涉，
//           next()
//         }
//     }
// })

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')
